﻿//没有来源网址去登录
if(!document.referrer){
    location.href="http://www.pulu.com/weixin/login.html";
}
if(!$.cookie("MYID")){
    location.href="http://www.pulu.com/weixin/login.html";
}
/*
 页面参数
 */
var currentIndex=1,pageSize=15,readyover=1;

/*
 页面返回 回主页
 */
$("#logo").click(function(){
    history.go(-1);
});

/*
 时间
 */
function getDateTimeStamp(dateStr){
    dateStr=dateStr.match(/[0-9\-T\:]+/)[0].replace(/T/g,' ');
    return Date.parse(dateStr.replace(/-/gi,"/"));
}
function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if(diffValue < 0){ result="刚刚";return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){result="" + parseInt(monthC) + "月前";}
    else if(weekC>=1){result="" + parseInt(weekC) + "周前";}
    else if(dayC>=1){result=""+ parseInt(dayC) +"天前";}
    else if(hourC>=1){result=""+ parseInt(hourC) +"小时前";}
    else if(minC>=1){result=""+ parseInt(minC) +"分钟前";}
    else result="刚刚";
    return result;
}

/*
 加载图片
 */
function showloadimg(e){
    $(e).fadeIn();
}

/*
 * 保存跳转页码 距离
 */
function savepage(){
    var scrollTop=$(window).scrollTop();
    $.cookie("kyscrollTop",scrollTop);
    $.cookie("kycurrentIndex",currentIndex);
}

//跳转经验类别
function goexptype(d){
    //取消事件冒泡
    var e=arguments.callee.caller.arguments[0]||event;
    if (e && e.stopPropagation) {
        e.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
    savepage();//保存页码 距离
    readyover=0;
    if(/^[0-9]+$/.test(d.dataset.type)){
        switch(parseInt(d.dataset.type)){
            case 1:$.cookie("selectType","");location.href="questionlistjy.html?type=1";break;//吃亏
            case 2:$.cookie("selectType","");location.href="questionlistjy.html?type=2";break;//划算
            case 3:$.cookie("selectType","");location.href="questionlistjy.html?type=3";break;//新奇
            case 4:$.cookie("selectType","");location.href="questionlistjy.html?type=4";break;//其他
            case 5:$.cookie("selectType","");location.href="questionlistjy.html?type=5";break;//电影
            case 6:$.cookie("selectType","");location.href="questionlistjy.html?type=6";break;//美食
            case 7:$.cookie("selectType","");location.href="questionlistjy.html?type=7";break;//旅游
            case 8:$.cookie("selectType","");location.href="questionlistjy.html?type=8";break;//读书
            case 22:
                $.cookie("selectType","");
                if(!is_weixn){location.href="questionlistjy.html?type=22&id="+$.cookie("MYID")}
                else{location.href="questionlistjy.html?type=22"};
                break;//附近
            case 23:$.cookie("selectType","");location.href="questionlistjy.html?type=23";break;//人生
            case 24:$.cookie("selectType","");location.href="questionlistjy.html?type=24";break;//育儿
            case 25:$.cookie("selectType","");location.href="questionlistjy.html?type=25";break;//工作
            case 26:$.cookie("selectType","");location.href="questionlistjy.html?type=26";break;//健康
            case 27:$.cookie("selectType","");location.href="questionlistjy.html?type=27";break;//购物
        }
    }else if(/^[a-z]+$/.test(d.dataset.type)){
        $.cookie("selectType","");
        readyover=0;
        location.href="questionlistjy.html?type="+d.dataset.type;
    }
}

/*
 获取数据
 */
if(!$.cookie("keywordid")||!/[0-9]+/.test($.cookie("keywordid"))){
    var keywordtype=location.href.match(/type=[0-9]+/)[0].split("=")[1];//类型
    $.cookie("keywordtype",keywordtype);
    var keywordid=location.href.match(/eid=[0-9]+/)[0].split("=")[1]; //经验ID
    $.cookie("keywordid",keywordid);
}
/*
 点击用户头像
 */
function usergohref(e){
    var even=arguments.callee.caller.arguments[0]||event;
    if (even && even.stopPropagation) {
        even.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
    if($.cookie("MYID")){
        savepage();
        var sendid="?otherid="+e.dataset.uid;
        if(!is_weixn()){sendid="?id="+$.cookie("MYID")+"&otherid="+e.dataset.uid;}
        if(e.dataset.uid==$.cookie("MYID")){
            location.href="setpage.html";
        }
        else{
            location.href="otherpeoplepage.html"+sendid;
        }
    }
}
function hrefgo(d){
    var url= d.dataset.href;
    savepage();//保存页码 距离
    //清除转发者信息 防止返回信息保存
    $.cookie("shareUserId","");
    $.cookie("fromuser","");
    var shareuserid="";
    if(d.dataset.shareuserid){
        $.cookie("shareUserId",d.dataset.shareuserid);
    }
    location.href=url+shareuserid;
}

/*
 刷新页面改变加载关键词
 */
function gotoheiexp(data){
    var eid=data.dataset.keyword;
    $.cookie("keywordid",eid);
    var e=arguments.callee.caller.arguments[0]||event;
    var loca=location.href;
    if(e&&e.stopPropagation()){e.stopPropagation();}else if(window.event){window.event.cancelBubble = true;}
    //页面初始化 清空
    $("#waiting1").css("display","block");
    $("#nonews1").css("display","none");
    $("#explist .addlistplace div").remove();
    $(window).scrollTop(0);
    currentIndex=1;
    pageSize=15;
    getallnote(currentIndex,pageSize);
}

/*
 经验方法
 */
function jy(d){
    //全部经验页 返回家在重复使用jyhaveajax进行判断 pulupagejy.js
    if(d.title&& d.title.length>0){
        //是不是推荐 //真名
        var tuijianword="";
        var realName="";
        if(d.realName){realName=" ("+d.realName+")"};
        if(d.isstanger===1){
            realName="";
            tuijianword="系统推荐 ";
        }
        //经验ID
        var eid="";
        if(d.id){eid=d.id}else if(d.qid){eid=d.qid};
        console.log(eid);
        //头像
        var uimgword="";
        if(d.questionUserAva){
            if(d.ishidename==1){
                uimgword="<a href='javascript: ;'><img class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)'></a>";
            }else{
                uimgword="<a onclick='usergohref(this)' data-uid='"+d.questionUserId+"'><img class='profile_photo_img' src='http://42.62.66.198:8003/"+d.questionUserAva+"' onload='showloadimg(this)' style='display:none;'></a>";
            }
        }else{
            if(d.ishidename==1){
                uimgword="<a href='javascript: ;'><img class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
            }else{
                uimgword="<a onclick='usergohref(this)' data-uid='"+d.questionUserId+"'><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
            }
        };
        //时间
        var tim="";
        if(d.shareTime){tim=getDateDiff(getDateTimeStamp(d.shareTime));}
        //有没有图片
        var haveimg="";
        if(d.imgcount>0||(d.qImgs&&d.qImgs.length>0)){haveimg="<span class='isheighlogo haveimg'></span>"}
        //详情页跳转地址
        var detailurl="shareexppage.html?eid="+eid+"#fromuser="+ $.cookie("MYID");

        var logoImg="";
        var keyTypeWord="的经验";
        var resTypeCode="";
        var resTypeName="";
        if(d.qtype==2){
            logoImg=" xs";
            keyTypeWord="的悬赏";
            detailurl="questiondetail.html?qid="+eid+"#fromuser="+ $.cookie("MYID");
        }else if(d.qtype==4){
            logoImg=" singlelogo";
            resTypeCode="single";
            resTypeName="单身资源";
            keyTypeWord="的单身资源";
        }else if(d.qtype==6){
            logoImg=" singlelogo";
            keyTypeWord="的工作介绍";
        }else if(d.qtype==7){
            logoImg=" singlelogo";
            keyTypeWord="的租房买房";
        }else if(d.qtype==8){
            logoImg=" singlelogo";
            resTypeCode="progect";
            resTypeName="项目资源";
            keyTypeWord="的项目资源";
        }else if(d.qtype==9){
            logoImg=" singlelogo";
            keyTypeWord="的人脉资源";
        }else if(d.qtype==10){
            logoImg=" singlelogo";
            resTypeCode="other";
            resTypeName="其他资源";
            keyTypeWord="的其他资源";
        }
        //付费数
        var payNum= d.payCount;
        //有没有钱
        var money="";
        var score="免费";
        if(d.score>0){score="¥ "+parseFloat(d.score)/100;}
        if(d.isredbag==1){score="红包";payNum=""}
        //if(d.qtype!==2){money="<span class='havemy jy"+logoImg+"'><i></i>"+score+"</span>";}
        //else{money="<span class='havemy xs'><b></b>"+score+"</span>";}
        if(d.qtype!==2){money="<span class='havemy jy"+logoImg+"'><i></i></span>";}
        else{money="<span class='havemy xs'><b></b></span>";}
        //是不是转换
        var isconvert="";
        if(d.isconvert&&d.isconvert===1){
            //isconvert="(悬赏完成转经验)"
        }

        //我也说说
        var gotoheiexp="<div class='gotoheiexp' onclick='gotoheiexp(this)' data-keyword="+eid+">"+keyTypeWord+"</div>";
        //高额
        var isheilogo="";
        if(d.isexphigh===1||d.isexphigh===2){
            isheilogo="<span class='isheighlogo'></span>";
        }
        //经验类型
        var exptypeword="";
        if(d.expTypeInfo&&d.expTypeInfo.length>0){
            for(var i=0;i<d.expTypeInfo.length;i++){
                exptypeword+='<span class="exptype" onclick="goexptype(this)" data-type="'+d.expTypeInfo[i].id+'">'+d.expTypeInfo[i].name+'</span>  ';
            }
            exptypeword="<span style='color:#598abf;'>"+exptypeword+"</span>";
        }else if(d.qtype==2){
            exptypeword="<span>悬赏</span>";
        }else{
            exptypeword='<span style="color:#598abf;"><span class="exptype" onclick="goexptype(this)" data-type="'+resTypeCode+'">'+resTypeName+'</span></span>  ';
        }
        //标题
        var tit=codetoface(d.title);
        var count=0;
        if(d.count){count=d.count;}
        //昵称
        var unick="";
        if(d.ishidename===1){
            unick="<a>"+tuijianword+"匿名</a>";
        }else{
            unick="<a onclick='usergohref(this)' data-uid='"+d.questionUserId+"'>"+tuijianword+"<i class='usernick'> "+d.questionUserNick+realName+"</i></a>";
        }
        //位置
        var location="";
        if(d.pululocation){
            location="<div style='position:relative;margin-bottom:6px;'><img src='img/dingwei.png' style='height:12px;position: absolute;bottom:2px;left:0;'/><span style='color:#999;padding-left:15px;font-size:12px;'>"+d.pululocation+"</span></div>"
        }
        //关键词
        var keywordnor;
        var keyword="<span class='keywordtit'>查看更多有关</span>";
        if(d.keywords){
            keywordnor=d.keywords.split(",");
            for(var a=0;a<keywordnor.length;a++){
                keyword+="<span>"+keywordnor[a]+"</span>";
            }
        }
        //多少字
        var wordNum="";
        if(d.wordcount){wordNum=d.wordcount}
        else if(d.WordCount){wordNum=d.WordCount};
        var des="";
        //不是转发
        if(!d.shareUserId){
            if(!tim){tim="刚刚";}
        }
        //转发
        else if(d.shareUserId&&d.shareUserId.length>0){
            if(d.des){des=codetoface(d.des);}
            var count=0;
            if(d.count){count=d.count;}
            var sharetim=getDateDiff(getDateTimeStamp(d.shareTime.match(/[0-9\-T\:]+/)[0]));
            if(!sharetim){sharetim="刚刚";}
            realName="";
            if(d.shareRealName){realName=" ("+d.shareRealName+")"};
            unick="<a onclick='usergohref(this)' data-uid='"+d.shareUserId+"'><i class='usernick'>"+d.shareUserNick+realName+"</i> 的转发</a>";
            if(d.shareUserAva){
                uimgword="<a onclick='usergohref(this)' data-uid='"+d.shareUserId+"'><img class='profile_photo_img' src='http://42.62.66.198:8003/"+d.shareUserAva+"' onload='showloadimg(this)' style='display:none;'></a>";
            }else{
                uimgword="<a onclick='usergohref(this)' data-uid='"+d.shareUserId+"'><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
            };
            var location="";
            if(d.pululocation){
                location="<div style='position:relative;'><img src='img/dingwei.png' style='height:12px;position: absolute;bottom:2px;left:0;'/><span style='color:#999;padding-left:15px;font-size:12px;'>"+d.pululocation+"</span></div>"
            }

            tim=sharetim;
        };
        if(d.isexphigh===0){
            isheilogo="";
        };
        //$("#explist .addlistplace").append("<div class='pagedlist_item shareexperience' data-money='"+d.score+"' onclick='hrefgo(this)' data-page='#mainbody' data-euser='"+d.questionUserId+"' data-href='"+detailurl+"' data-shareuserid='"+d.shareUserId+"'><div class='feed_item feed_type_question'><div class='borderbox'>"+"<div class='ContentHeader'><div class='photo_info_layout size_small'><div class='photo_wrapper'><div class='photo'>"+uimgword+"</div></div><div class='info_wrapper'><span class='feed_item_answer_user'> <span class='user'>"+unick+"</span>"+money+"</span><div class='ContentFooter'><span style='color:#598abf;'>"+exptypeword+"</span>"+tim+"</div></div></div></div>"+"<div class='info_wrapper' style='margin-bottom:7px;'> <div class='info'> <span class='feed_item_answer_user'>"+des+"</span> </div> </div> <h2><a class='question_link'>"+isconvert+tit+"("+wordNum+"字)"+haveimg+isheilogo+"</a></h2><div class='ActionBar Question'>"+location+"<div class='question_topics_container' onclick='gotoheiexp(this)' data-keyword="+eid+"><div class='QuestionTopics has_topics'>"+keyword+gotoheiexp+"</div></div><div class='action_bar_inner'><div class='action_item'> <a class='ReadMoreActionItem'>"+count+" 浏览 · "+payNum+ d.webchatCount+" 转发到微信</a></div></div></div>"+"</div></div></div>")

        /*newStyle*/
        if(d.pululocation){
            location="<p style='position:relative;margin-bottom:4px;margin-top:8px;'><p class='locationLogo' style='position: absolute;bottom:11px;left:0;'></p><span style='color:#999;padding-left:15px;font-size:13px;'>"+d.pululocation+"</span></div>"
        };
        var newKeyWord="<span class='newKeyWord' onclick='gotoheiexp(this)' data-qtype='"+d.qtype+"' data-keyword='"+eid+"'>"+d.keywords.split(",").join("|")+"|    </span>";
        if(d.isredbag===1){score="红包";payNum=""}else{
            payNum="&nbsp;&nbsp;&nbsp;&nbsp;<span class='payNum'></span>"+payNum;
        }

        var displayNone='style="display:none;"';
        if(d.isShowToMe===1){displayNone=""};
        $("#explist .addlistplace").append(
            "<div class='pagedlist_item shareexperience' onclick='hrefgo(this)' data-euser='"+d.questionUserId+"' data-href='"+detailurl+"' data-shareuserid='"+d.shareUserId+"'>" +
            "<div class='feed_item feed_type_question'>" +
            "<div class='borderbox'>"+
            "<div class='ContentHeader' "+displayNone+">" +
            "<div class='photo_info_layout size_small'>" +
            "<div class='photo_wrapper'>" +
            "<div class='photo'>"+uimgword+"</div>" +
            "</div>" +
            "<div class='info_wrapper'>" +
            "<span class='user'>"+unick+"</span>"+
                //money+
            "<div class='ContentFooter' style='display:block;'>" +
            unick+"花费"+parseFloat(d.showMoney/100)+"显示给我看"+
            "</div>" +
            "</div>" +
            "</div>" +
            "</div>"+

            "<div class='info_wrapper' style='margin-bottom:7px;'> " +
            "<div class='info'> <span class='feed_item_answer_user'>"+des+"</span> </div> " +
            "</div> " +
            "<h1>"+
            money+
            newKeyWord+
            "<a class='question_link'>"+isconvert+tit+"("+d.WordCount+"字)"+haveimg+isheilogo+"</a>" +
            "</h1>" +
            "<div class='ActionBar Question'>" +
            "<div class='action_bar_inner'>" +
            "<div class='action_item'> " +
            "<a class='ReadMoreActionItem'>" +
            "<span class='scoreNum'>"+score+"</span>"+
            "<span class='browseNum'></span>"+
            count+
                //payNum+"&nbsp;&nbsp;&nbsp;&nbsp;" +
            "<span class='sendNum'></span>"+
            d.webchatCount+
            "</a>" +
            "<a class='rt'>"+tim+"</a>" +
            "</div>" +
            "</div>"+
            location+
            "</div>"+
            "</div>" +
            "</div>" +
            "</div>"
        )
        //$("#explist .addlistplace").append("<div class='pagedlist_item shareexperience' onclick='hrefgo(this)' data-euser='"+d.questionUserId+"' data-href='"+detailurl+"' data-shareuserid='"+d.shareUserId+"'><div class='feed_item feed_type_question'><div class='borderbox'>"+"<div class='ContentHeader'><div class='photo_info_layout size_small'><div class='photo_wrapper'><div class='photo'>"+uimgword+"</div></div><div class='info_wrapper'><span class='feed_item_answer_user'> <span class='user'>"+unick+"</span>"+money+"</span><div class='ContentFooter'>"+exptypeword+"</div></div></div></div>"+"<div class='info_wrapper' style='margin-bottom:7px;'> <div class='info'> <span class='feed_item_answer_user'>"+des+"</span> </div> </div> <h1>"+newKeyWord+"<a class='question_link'>"+isconvert+tit+"("+wordNum+"字)"+haveimg+isheilogo+"</a></h1><div class='ActionBar Question'><div class='action_bar_inner'><div class='action_item'> <a class='ReadMoreActionItem'><span class='browseNum'></span>"+count+payNum+"&nbsp;&nbsp;&nbsp;&nbsp;<span class='sendNum'></span>"+d.webchatCount+"</a><a class='rt'>"+tim+"</a></div></div>"+location+"</div>"+"</div></div></div>")


    };
};

//加载经验
function getExpTypenote(currentIndex,pageSize){
    $.ajax({
        type:"get",
        url:keywordlist+"?userId="+$.cookie("MYID")+"&currentIndex="+currentIndex+"&pageSize="+pageSize+"&qtype="+$.cookie("keywordtype")+"&qid="+$.cookie("keywordid"),
        success:function(d){
            console.log(d);
            readyover=0;
            console.log(d.Data.length+":"+pageSize);
            if(d.Message=="没有数据"||!d.Data||d.Data.length==0){
                $("#waiting1").css("display","none");
                $("#nonews1").css("display","block");
            }
            else{
                if(d.Data.length<pageSize){
                    $("#waiting1").css("display","none");
                    $("#nonews1").css("display","block");
                }
                for(var i=0;i< d.Data.length;i++){
                    jy(d.Data[i]);
                }
                //页面定位
                if($.cookie("kycurrentIndex")&&/[0-9]/.test($.cookie("kycurrentIndex"))){
                    if(/^[0-9]+$/.test($.cookie("kyscrollTop"))){
                        var toTop= parseInt($.cookie("kyscrollTop"));
                        $(window).scrollTop(toTop);
                        //还原参数
                        pageSize=15;
                        currentIndex=parseInt($.cookie("kycurrentIndex"));
                        //清除参数
                        $.cookie("kyscrollTop","");
                        $.cookie("kycurrentIndex","");
                    }
                }
            }
        }
    })
}

/*
 调用接口
 */
function getallnote(currentIndex,pageSize){
    console.log("调用");
    //公用参数 页面加载类型
    if(!$.cookie("keywordid")){ //获取关键词对应经验的qid
        var typeId=location.href.match(/type=[0-9]+/)[0].split("=")[1];
        $.cookie("keywordid",typeId);
    }
    readyover=1;
    //判断类型 经验/单身...
    if(/^[0-9]+$/.test($.cookie("keywordid"))){
        //纯数字 为经验的类型
        getExpTypenote(currentIndex,pageSize);
    }

}

//经验数据
if(!$.cookie("MYID")){
    location.href="http://www.pulu.com/weixin/login.html"
}
if($.cookie("kycurrentIndex")){
    $("#waiting1").css("display","block");
    $("#nonews1").css("display","none");
    $("#explist .addlistplace div").remove();
    readyover=1;//控制页面滚动
    //判断页码是否为数字
    if(/[0-9]/.test($.cookie("kycurrentIndex"))){
        pageSize=parseFloat($.cookie("kycurrentIndex"))*pageSize;
        currentIndex=1;
    } else{
        pageSize=15;
        currentIndex=1;
    }
    getallnote(1,pageSize);
}
else{
    if($.cookie("MYID")){
        //经验
        if(/type=[0-9]+/.test(location.href)){
            getallnote(currentIndex,pageSize);
        }
    }
}

/*
 滚动
 */
$(window).scroll(function(){
    $(".quicklist").css("display","none");
    $("#refreshbtn").css("display","block");
    var mainheight=$(".home_feed").height();
    var windowheight=$(window).height();
    var what=$(".home_feed").offset().top;
    var chajuli=$(".home_feed").offset().top - $(window).scrollTop();
    var cz=windowheight-mainheight+50;
    if(chajuli<cz&&readyover===0){
        currentIndex++;
        readyover=1;
        getallnote(currentIndex,pageSize);
    }
});